package com.burstly.lib.currency.request;

import android.os.Handler;
import android.os.Looper;
import com.burstly.lib.currency.AccountInfo;
import com.burstly.lib.currency.BalanceUpdateInfo;
import com.burstly.lib.currency.UserAccount;
import com.burstly.lib.feature.currency.ICurrencyListener;
import com.burstly.lib.util.LoggerExt;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public final class CurrencyRequester
{
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final int NANOS_IN_SEC = 1000000000;
  private static final String TAG = CurrencyRequester.class.getSimpleName();
  private static final int THROTTLE_INTERVAL = 10;
  private static final Map<String, Long> THROTTLE_MAP = new HashMap(5);
  Executor mExecutor = Executors.newSingleThreadExecutor();
  final List<ICurrencyListener> mListeners;
  final Lock mResetLock = new ReentrantLock();
  final Condition mResetLockCondition = this.mResetLock.newCondition();
  final Handler mUIHandler = new Handler(Looper.getMainLooper());

  public CurrencyRequester(List<ICurrencyListener> paramList)
  {
    this.mListeners = paramList;
  }

  private static boolean isThrottled(AccountInfo paramAccountInfo)
  {
    boolean bool = false;
    while (true)
    {
      try
      {
        String str = paramAccountInfo.getUserId() + paramAccountInfo.getPubId();
        Long localLong = (Long)THROTTLE_MAP.get(str);
        if (localLong == null)
        {
          THROTTLE_MAP.put(str, Long.valueOf(System.nanoTime()));
          return bool;
        }
        if ((System.nanoTime() - localLong.longValue()) / 1000000000L > 10L)
        {
          THROTTLE_MAP.remove(str);
          bool = false;
          continue;
        }
      }
      finally
      {
      }
      LOG.logDebug(TAG, "Update balance request for {0} user is throttled!", new Object[] { paramAccountInfo });
      bool = true;
    }
  }

  private void notifyListeners(Map<String, BalanceUpdateInfo> paramMap, boolean paramBoolean)
  {
    if (!this.mListeners.isEmpty());
    while (true)
    {
      ICurrencyListener localICurrencyListener;
      try
      {
        Iterator localIterator = this.mListeners.iterator();
        if (localIterator.hasNext())
        {
          localICurrencyListener = (ICurrencyListener)localIterator.next();
          if (paramBoolean)
            break label72;
          localICurrencyListener.didFailToUpdateBalance(paramMap);
          continue;
        }
      }
      catch (Exception localException)
      {
        LOG.logThrowable(TAG, localException);
      }
      return;
      label72: localICurrencyListener.didUpdateBalance(paramMap);
    }
  }

  private void notifySuccess(Map<String, BalanceUpdateInfo> paramMap)
  {
    notifyListeners(paramMap, true);
  }

  public void requestBalanceReset(final AccountInfo paramAccountInfo)
  {
    this.mExecutor.execute(new Runnable()
    {
      volatile boolean mResetFinished;

      public void run()
      {
        try
        {
          CurrencyRequester.this.mResetLock.lock();
          CurrencyRequester.this.mUIHandler.post(new RequestBalanceResetRunnable(paramAccountInfo, CurrencyRequester.this.mListeners)
          {
            protected void onResetFinished()
            {
              super.onResetFinished();
              CurrencyRequester.3.this.mResetFinished = true;
              try
              {
                CurrencyRequester.this.mResetLock.lock();
                CurrencyRequester.this.mResetLockCondition.signalAll();
                return;
              }
              finally
              {
                CurrencyRequester.this.mResetLock.unlock();
              }
            }
          });
          while (!this.mResetFinished)
            CurrencyRequester.this.mResetLockCondition.awaitUninterruptibly();
        }
        finally
        {
          CurrencyRequester.this.mResetLock.unlock();
        }
        CurrencyRequester.this.mResetLock.unlock();
      }
    });
  }

  public void requestBalanceUpdate(final UserAccount paramUserAccount, final AccountInfo paramAccountInfo)
  {
    if (isThrottled(paramAccountInfo))
    {
      notifySuccess(BalanceUpdateInfo.createFrom(paramUserAccount.getAllCurrencyAccounts()));
      return;
    }
    this.mExecutor.execute(new Runnable()
    {
      public void run()
      {
        CurrencyRequester.this.mUIHandler.post(new RequestBalanceUpdateRunnable(paramUserAccount, paramAccountInfo, CurrencyRequester.this.mListeners));
      }
    });
  }

  public void requestLinkAccount(final UserAccount paramUserAccount, final AccountInfo paramAccountInfo)
  {
    this.mExecutor.execute(new Runnable()
    {
      public void run()
      {
        CurrencyRequester.this.mUIHandler.post(new RequestLinkAccountRunnable(paramUserAccount, paramAccountInfo));
      }
    });
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.currency.request.CurrencyRequester
 * JD-Core Version:    0.6.2
 */